package endpoint

import (
	"context"
	service "mytest/kitcli/user/pkg/service"
	"mytest/kitcli/user/pkg/transport/grpc/pb"

	endpoint "github.com/go-kit/kit/endpoint"
)

// GetUserListRequest collects the request parameters for the GetUserList method.
type GetUserListRequest struct {
	Request *pb.GetUserListRequest `json:"request"`
}

// GetUserListResponse collects the response parameters for the GetUserList method.
type GetUserListResponse struct {
	Result interface{} `json:"result"`
	Err    error       `json:"err"`
}

// MakeGetUserListEndpoint returns an endpoint that invokes GetUserList on the service.
func MakeGetUserListEndpoint(s service.UserService) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(GetUserListRequest)
		result, err := s.GetUserList(ctx, req.Request)
		return GetUserListResponse{
			Err:    err,
			Result: result,
		}, nil
	}
}

// Failed implements Failer.
func (r GetUserListResponse) Failed() error {
	return r.Err
}

// AddUserRequest collects the request parameters for the AddUser method.
type AddUserRequest struct {
	Request *pb.AddUserRequest `json:"request"`
}

// AddUserResponse collects the response parameters for the AddUser method.
type AddUserResponse struct {
	Result interface{} `json:"result"`
	Err    error       `json:"err"`
}

// MakeAddUserEndpoint returns an endpoint that invokes AddUser on the service.
func MakeAddUserEndpoint(s service.UserService) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(AddUserRequest)
		result, err := s.AddUser(ctx, req.Request)
		return AddUserResponse{
			Err:    err,
			Result: result,
		}, nil
	}
}

// Failed implements Failer.
func (r AddUserResponse) Failed() error {
	return r.Err
}

// UpdateUserRequest collects the request parameters for the UpdateUser method.
type UpdateUserRequest struct {
	Request *pb.UpdateUserRequest `json:"request"`
}

// UpdateUserResponse collects the response parameters for the UpdateUser method.
type UpdateUserResponse struct {
	Result interface{} `json:"result"`
	Err    error       `json:"err"`
}

// MakeUpdateUserEndpoint returns an endpoint that invokes UpdateUser on the service.
func MakeUpdateUserEndpoint(s service.UserService) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(UpdateUserRequest)
		result, err := s.UpdateUser(ctx, req.Request)
		return UpdateUserResponse{
			Err:    err,
			Result: result,
		}, nil
	}
}

// Failed implements Failer.
func (r UpdateUserResponse) Failed() error {
	return r.Err
}

// DeleteUserRequest collects the request parameters for the DeleteUser method.
type DeleteUserRequest struct {
	Request *pb.DeleteUserRequest `json:"request"`
}

// DeleteUserResponse collects the response parameters for the DeleteUser method.
type DeleteUserResponse struct {
	Result interface{} `json:"result"`
	Err    error       `json:"err"`
}

// MakeDeleteUserEndpoint returns an endpoint that invokes DeleteUser on the service.
func MakeDeleteUserEndpoint(s service.UserService) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(DeleteUserRequest)
		result, err := s.DeleteUser(ctx, req.Request)
		return DeleteUserResponse{
			Err:    err,
			Result: result,
		}, nil
	}
}

// Failed implements Failer.
func (r DeleteUserResponse) Failed() error {
	return r.Err
}

// FindUserRequest collects the request parameters for the FindUser method.
type FindUserRequest struct {
	Request *pb.FindUserRequest `json:"request"`
}

// FindUserResponse collects the response parameters for the FindUser method.
type FindUserResponse struct {
	Result interface{} `json:"result"`
	Err    error       `json:"err"`
}

// MakeFindUserEndpoint returns an endpoint that invokes FindUser on the service.
func MakeFindUserEndpoint(s service.UserService) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(FindUserRequest)
		result, err := s.FindUser(ctx, req.Request)
		return FindUserResponse{
			Err:    err,
			Result: result,
		}, nil
	}
}

// Failed implements Failer.
func (r FindUserResponse) Failed() error {
	return r.Err
}

// Failure is an interface that should be implemented by response types.
// Response encoders can check if responses are Failer, and if so they've
// failed, and if so encode them using a separate write path based on the error.
type Failure interface {
	Failed() error
}

// GetUserList implements Service. Primarily useful in a client.
func (e Endpoints) GetUserList(ctx context.Context, request *pb.GetUserListRequest) (result interface{}, err error) {
	request0 := GetUserListRequest{Request: request}
	response, err := e.GetUserListEndpoint(ctx, request0)
	if err != nil {
		return
	}
	return response.(GetUserListResponse).Result, response.(GetUserListResponse).Err
}

// AddUser implements Service. Primarily useful in a client.
func (e Endpoints) AddUser(ctx context.Context, request *pb.AddUserRequest) (result interface{}, err error) {
	request0 := AddUserRequest{Request: request}
	response, err := e.AddUserEndpoint(ctx, request0)
	if err != nil {
		return
	}
	return response.(AddUserResponse).Result, response.(AddUserResponse).Err
}

// UpdateUser implements Service. Primarily useful in a client.
func (e Endpoints) UpdateUser(ctx context.Context, request *pb.UpdateUserRequest) (result interface{}, err error) {
	request0 := UpdateUserRequest{Request: request}
	response, err := e.UpdateUserEndpoint(ctx, request0)
	if err != nil {
		return
	}
	return response.(UpdateUserResponse).Result, response.(UpdateUserResponse).Err
}

// DeleteUser implements Service. Primarily useful in a client.
func (e Endpoints) DeleteUser(ctx context.Context, request *pb.DeleteUserRequest) (result interface{}, err error) {
	request0 := DeleteUserRequest{Request: request}
	response, err := e.DeleteUserEndpoint(ctx, request0)
	if err != nil {
		return
	}
	return response.(DeleteUserResponse).Result, response.(DeleteUserResponse).Err
}

// FindUser implements Service. Primarily useful in a client.
func (e Endpoints) FindUser(ctx context.Context, request *pb.FindUserRequest) (result interface{}, err error) {
	request0 := FindUserRequest{Request: request}
	response, err := e.FindUserEndpoint(ctx, request0)
	if err != nil {
		return
	}
	return response.(FindUserResponse).Result, response.(FindUserResponse).Err
}
